
// 有效的字母异位词
// Created by madison on 2022/8/14.
//

#include "string"
#include "vector"

using namespace std;

class Solution {
public:
    // 方法一：排序
    bool isAnagram(string s, string t) {
        if (s.length() != t.length()) {
            return false;
        }
        sort(s.begin(), s.end());
        sort(t.begin(), t.end());
        return s == t;
    }

    // 方法二：哈希表
    bool isAnagram1(string s, string t) {
        if (s.length() != t.length()) {
            return false;
        }
        vector<int> table(26, 0);
        for (auto &ch: s) {
            table[ch - 'a']++;
        }
        for (auto &ch: t) {
            table[ch - 'a']--;
            if (table[ch - 'a'] < 0) {
                return false;
            }
        }
        return true;
    }
};

int main() {
    Solution solution;
    printf("%d\n", solution.isAnagram1("anagram", "nagaram"));
    printf("%d\n", solution.isAnagram1("rat", "car"));
}